---
keywords: ""
layout: "default"
title: "UnicodeCodec"
description: "Swift documentation for 'UnicodeCodec'"
root: "/v5.1"
---

<div class="intro-declaration"><code class="language-swift">protocol UnicodeCodec</code></div><p>A Unicode encoding form that translates between Unicode scalar values and
form-specific code units.</p>
<table class="standard"><tr><th id="inheritance">Inheritance</th><td><code class="inherits">_UnicodeEncoding</code></td></tr><tr><th id="conforming-types">Conforming Types</th><td><code class="inherits">Unicode.UTF16, Unicode.UTF32, Unicode.UTF8</code></td></tr></table><div class="discussion comment"><p>The <code>UnicodeCodec</code> protocol declares methods that decode code unit
sequences into Unicode scalar values and encode Unicode scalar values
into code unit sequences. The standard library implements codecs for the
UTF-8, UTF-16, and UTF-32 encoding schemes as the <code>UTF8</code>, <code>UTF16</code>, and
<code>UTF32</code> types, respectively. Use the <code>Unicode.Scalar</code> type to work with
decoded Unicode scalar values.</p>
</div><h3>Initializers</h3><div id="init-e67906ab4373125a18eb2b5a75f59bd2" class="declaration"><a class="toggle-link" href="#comment-init-e67906ab4373125a18eb2b5a75f59bd2">init init()</a> <span class="required">Required</span><div class="comment collapse in" id="comment-init-e67906ab4373125a18eb2b5a75f59bd2"><p>Creates an instance of the codec.</p>
<h4>Declaration</h4><code class="language-swift">init()</code></div></div><h3>Instance Methods</h3><div id="decode-8025df040b7adc85d770a524cec1c450" class="declaration"><a class="toggle-link" href="#comment-decode-8025df040b7adc85d770a524cec1c450">func decode(_ input: inout I) -> UnicodeDecodingResult</a> <span class="required">Required</span><div class="comment collapse in" id="comment-decode-8025df040b7adc85d770a524cec1c450"><p>Starts or continues decoding a code unit sequence into Unicode scalar
values.</p>
<p>To decode a code unit sequence completely, call this method repeatedly
until it returns <code>UnicodeDecodingResult.emptyInput</code>. Checking that the
iterator was exhausted is not sufficient, because the decoder can store
buffered data from the input iterator.</p>
<p>Because of buffering, it is impossible to find the corresponding position
in the iterator for a given returned <code>Unicode.Scalar</code> or an error.</p>
<p>The following example decodes the UTF-8 encoded bytes of a string into an
array of <code>Unicode.Scalar</code> instances:</p>
<pre><code class="language-swift">let str = &quot;✨Unicode✨&quot;
print(Array(str.utf8))
// Prints &quot;[226, 156, 168, 85, 110, 105, 99, 111, 100, 101, 226, 156, 168]&quot;

var bytesIterator = str.utf8.makeIterator()
var scalars: [Unicode.Scalar] = []
var utf8Decoder = UTF8()
Decode: while true {
    switch utf8Decoder.decode(&amp;bytesIterator) {
    case .scalarValue(let v): scalars.append(v)
    case .emptyInput: break Decode
    case .error:
        print(&quot;Decoding error&quot;)
        break Decode
    }
}
print(scalars)
// Prints &quot;[&quot;\u{2728}&quot;, &quot;U&quot;, &quot;n&quot;, &quot;i&quot;, &quot;c&quot;, &quot;o&quot;, &quot;d&quot;, &quot;e&quot;, &quot;\u{2728}&quot;]&quot;
</code></pre>
<ul>
<li>Parameter input: An iterator of code units to be decoded. <code>input</code> must be
the same iterator instance in repeated calls to this method. Do not
advance the iterator or any copies of the iterator outside this
method.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">mutating func decode&lt;I>(_ input: inout I) -> UnicodeDecodingResult where I: IteratorProtocol, Self.CodeUnit == I.Element</code></div></div><h3>Type Methods</h3><div id="encode_into-0766aad6a30ce928eafdd99694fb9fed" class="declaration"><a class="toggle-link" href="#comment-encode_into-0766aad6a30ce928eafdd99694fb9fed">func encode(_ input: Unicode.Scalar, into processCodeUnit: (Self.CodeUnit) -> Void)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-encode_into-0766aad6a30ce928eafdd99694fb9fed"><p>Encodes a Unicode scalar as a series of code units by calling the given
closure on each code unit.</p>
<p>For example, the musical fermata symbol (&quot;𝄐&quot;) is a single Unicode scalar
value (<code>\u{1D110}</code>) but requires four code units for its UTF-8
representation. The following code uses the <code>UTF8</code> codec to encode a
fermata in UTF-8:</p>
<pre><code class="language-swift">var bytes: [UTF8.CodeUnit] = []
UTF8.encode(&quot;𝄐&quot;, into: { bytes.append($0) })
print(bytes)
// Prints &quot;[240, 157, 132, 144]&quot;
</code></pre>
<h4>Declaration</h4><code class="language-swift">static func encode(_ input: Unicode.Scalar, into processCodeUnit: (Self.CodeUnit) -> Void)</code></div></div>